package com.hejielife.threadlocal;

/**
 * 2019年8月1日 下午3:54:22
 * 
 * @author hejielife
 * @despribe ThreadLocal，实现线程变量的隔离
 */
public class ConnThreadLocal {

	public static ThreadLocal<String> th = new ThreadLocal<String>();

	public void setTh(String value) {
		th.set(value);
	}

	public void getTh() {
		System.out.println(Thread.currentThread().getName() + ":" + this.th.get());
	}

	public static void main(String[] args) throws InterruptedException {

		final ConnThreadLocal ct = new ConnThreadLocal();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				ct.setTh("张三");
				ct.getTh();
			}
		}, "t1");

		Thread t2 = new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(1000);
					ct.getTh();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "t2");
		t1.start();
		t2.start();
	}
}
